﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class PolicyGateway:DBGateway
    {
        public string SaveNewPolicy(Policy policy)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =
                    string.Format("insert into tblPolicy values(@PolicyName,@DiscountAmount,@NumberofJobQuota,@Description,@FreePostingDay)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@PolicyName", SqlDbType.NVarChar).Value = policy.PolicyName;
                CommandObj.Parameters.Add("@DiscountAmount", SqlDbType.Int).Value = policy.DiscountAmount;
                CommandObj.Parameters.Add("@NumberofJobQuota", SqlDbType.NVarChar).Value = policy.NumberofJobQuota;
                CommandObj.Parameters.Add("@Description", SqlDbType.NVarChar).Value = policy.Description;
                CommandObj.Parameters.Add("@FreePostingDay", SqlDbType.Int).Value = policy.FreePostingDay;
                CommandObj.ExecuteNonQuery();
                return "New policy created successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }
        public List<Policy> SaveNewPolicy()
        {
        
            List<Policy>policies=new List<Policy>();
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =
                    string.Format("Seletc * From tblPolicy");
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Policy policy =new Policy();
                    policy.Id =Convert.ToInt32(reader[0]);
                    policies.Add(policy);
                }
                CommandObj.ExecuteNonQuery();
                
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return policies;
        }
    }
}